package com.example.mbl.service.impl;

import com.example.mbl.dao.ArticleMapper;
import com.example.mbl.entity.Article;
import com.example.mbl.result.PageResult;
import com.example.mbl.result.WebRestResult;
import com.example.mbl.result.business.ArticlePageResult;
import com.example.mbl.service.ArticleService;
import com.example.mbl.util.Tools;
import com.example.mbl.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public List<Article> searchItemsByKeyWord(String keyword) {
        return articleMapper.searchItemsByKeyWord(keyword);
    }

    @Override
    public WebRestResult addArticle(Article article) {
        WebRestResult result = new WebRestResult();
        article.setArticleid(UUIDUtil.randomUUID());
        if(Tools.isEmpty(article.getFlag()))
            article.setFlag("0");
        article.setCreatedate(new Date());
        if(articleMapper.insert(article)==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("新增文章失败");
        }
        return result;
    }

    @Override
    public WebRestResult deleteArticle(Article article) {
        WebRestResult result = new WebRestResult();
        if(articleMapper.deleteByPrimaryKey(article.getArticleid())==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("删除文章失败");
        }
        return result;
    }

    @Override
    public WebRestResult updateArticle(Article article) {
        WebRestResult result = new WebRestResult();
        if(articleMapper.updateByPrimaryKeySelective(article)==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("更新文章失败");
        }
        return result;
    }

    @Override
    public WebRestResult setArticleVisible(Article article) {
        WebRestResult result = new WebRestResult();
        if(articleMapper.setArticleVisible(article.getArticleid())==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("设置失败");
        }
        return result;
    }

    @Override
    public PageResult<ArticlePageResult> getArticleList(String title, String publishdate, int page, int pagesize) {
        PageResult<ArticlePageResult> result = new PageResult<>();
        int start = (page-1) * pagesize;

        List<ArticlePageResult> list = articleMapper.getArticleList(title,publishdate,start,pagesize);
        if(list.size()>0){
            result.setTotal(list.get(0).getCnt());
            result.setPageCount(list.get(0).getPage());
            result.setRows(list);
        }
        result.setResult(WebRestResult.SUCCESS);
        return result;
    }
}
